package com.von.nhcf.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.von.nhcf.entity.Role;
import com.von.nhcf.service.RoleService;
import com.von.nhcf.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Role控制器
 * @author
 */
@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 根据roleId删除一个或者多条role记录
     * @param roleIdList
     * @return
     */
    @RequestMapping("role/do/delete.json")
    @ResponseBody
    public ResultEntity deleteRole(@RequestBody List<Integer> roleIdList) {
        roleService.deleteRole(roleIdList);

        return ResultEntity.successWithoutData();
    }
    /**
     * 执行role对象的更新操作
     * @param role
     * @return
     */
    @RequestMapping("role/do/update.json")
    @ResponseBody
    public ResultEntity updateRole(Role role) {
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }
    /**
     * 执行role对象保存操作
     * @param role
     * @return ：如果返回值不为ResultEntity，
     * 那么role.js文件中的getPageInfoRemote函数获取不到responesJson，从而报错parsererror
     */
    @RequestMapping("role/do/save.json")
    @ResponseBody
    public ResultEntity saveRole(Role role) {
        roleService.saveRole(role);
        return ResultEntity.successWithoutData();
    }
    /**
     * 采用Ajax异步获取分页
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return : 分页成功消息
     */
    @ResponseBody
    @RequestMapping("/role/get/paging.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(
            @RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
            @RequestParam(value="keyword", defaultValue="") String keyword) {
        // 调用 Service 方法获取分页数据
        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
        // 封装到 ResultEntity 对象中返回（如果上面的操作抛出异常， 交给异常映射机制处理）
        return ResultEntity.successWithData(pageInfo);
    }
}
